// 无广告特征定义
var getNoAdFeature = function() {
    // return id("djx_drama_card_speaker").exists();
    return id("iv_top_coin").exists();
    // 28,478,58,508  金蛋
};

function 我爱短剧(gameConfig) {
    try {
        var module = "我爱短剧";
        Logger.debug(module, "开始执行我爱短剧函数 - 游戏：" + (gameConfig ? gameConfig.名称 : "未知"));

        // // 使用带once属性的点击函数 赚现金任务
        // Logger.debug(module, "点击赚现金");
        // if(clickWithOnce(540, 2137, gameConfig)) {  // 赚现金
        //     randomSleep(2800,3000);

        //     if (humanClick(543,1478)) {     // 翻倍赚 偏下
        //         readyForAction(); 
        //         randomSleep(2500,3800);  
        //     }
        //     if (humanClick(526,1668)) { //开心收下
        //         randomSleep(500,800);  
        //     }
        //     readyForAction(); 
        //     // 看广告赚金条
        //     var rect = bounds(66,1180,354,1245).visibleToUser(true).findOnce();
        //     if(rect) {
        //         humanClick(rect.centerX(), rect.centerY());
        //         randomSleep(1500,1800); 
        //         readyForAction(); 
        //     }else {
        //         humanClick(548,1499);  // 我知道了
        //     }
        //     randomSleep(1500,1800); 
        //     // 短剧按钮
        //     var rect = bounds(72,2076,144,2148).visibleToUser(true).findOnce();
        //     if(rect) {
        //         clickWithOnce(108, 2137, gameConfig);  // 短剧按钮
        //         randomSleep(1200,1500);
        //     }

        // }

        swipeWithOnce(567,444,555,1277, gameConfig);  // 下滑刷新
        randomSleep(2200,2500);

        Logger.debug(module, "点击推荐剧集");
        if(clickWithOnce(400, 767, gameConfig)) {  //推荐剧集
            randomSleep(1200,1500);  
        }
        
        Logger.debug(module, "点击开心收下");
        if (gameClick(547,1514)) { //开心收下
            randomSleep(500,800);  
        }
        
        // 点击
        rect = bounds(180,1721,900,1901).visibleToUser(true).findOnce();
        if(rect) {
            humanclick(rect.centerX(), rect.centerY());
            randomSleep(1000, 1500);
        }


        Logger.debug(module, "上滑操作");
        if (gameSwipe(652,1824,672,767,1000)) { //上滑
            randomSleep(500,800);  
        }

        Logger.debug(module, "概率点击红心");
        probabilityClick(1002, 1302, 0.15); // 概率点击红心
        randomSleep(150,300);  // 从200,500缩短到150,300
        
        Logger.debug(module, "概率点击收藏");
        probabilityClick(998, 1551, 0.05); // 概率点击收藏
        randomSleep(150,300);  // 从200,500缩短到150,300
        
        Logger.debug(module, "再次上滑");
        if (gameSwipe(652,1824,672,767,1000)) { //上滑
            randomSleep(500,800);  // 从1200,1500缩短到500,800
        }
        
        Logger.debug(module, "立即领取");
        humanClick(540,1812);
        randomSleep(1200,1500);

        Logger.debug(module, "点击解锁并领取");
        humanClick(607,1462);
        randomSleep(1200,1500);
        Logger.debug(module, "我爱短剧函数执行完成");
        return true;
    } catch (e) {
        Logger.debug("我爱短剧", "我爱短剧执行异常: " + e.message);
        return false;
    }
}

// 游戏提现函数
function 我爱短剧T(gameConfig) {
    try {
        var module = "我爱短剧";

        gameClick(633, 220); // 首页提现按钮
        readyForAction(); 
        randomSleep(2000,3000);  
        
        // 为不同游戏定义不同的余额提取区域坐标
        var balanceBoundsConfig = {
            "默认": [96,569,375,749],
            "浮光剧场": [96,569,375,749],
            "秒看剧场": [96,569,375,749],
            "赚剧时刻": [96,569,375,749],
            "来点短剧": [93,553,364,728],
            "金剧通": [93,553,364,728],
        };
        
        // 根据游戏名称选择对应的余额提取区域坐标
        var bounds = balanceBoundsConfig[gameConfig.名称] || balanceBoundsConfig["默认"];
        Logger.debug(module, "尝试提取余额信息，坐标范围: [" + bounds.join(",") + "]");
        // 使用解构参数调用extractTextByUI
        var textResult = extractTextByUI(bounds[0], bounds[1], bounds[2], bounds[3]);
         
        // 检查余额提取是否成功
        var currentBalance = 0;
        if (textResult && textResult.success && textResult.text) {
            Logger.debug(module, "成功获取文本内容: " + textResult.text);
            // 从文本中提取数字
            var balanceText = textResult.text.replace(/[^\d.]/g, ''); // 只保留数字和小数点
            currentBalance = parseFloat(balanceText) || 0;
            Logger.debug(module, "当前余额: " + currentBalance + " (原始文本: " + textResult.text + ", 提取后: " + balanceText + ")");
        } else {
            Logger.debug(module, "无法提取余额信息，使用默认值0");
            Logger.debug(module, "textResult对象: " + JSON.stringify(textResult));
            currentBalance = 0;
        }

        // 根据游戏名称定义不同的坐标配置
        var gameBoundsConfig = {
            "默认": [                                          
                {bounds:[873,1147,1008,1192],balance:[759,1190,917,1292]},             
                {bounds:[547,1147,682,1192],balance:[433,1190,591,1292]}, 
                {bounds:[221,1147,356,1192],balance:[129,1190,242,1292]},                     
                {bounds:[873,930,1008,975],balance:[781,973,894,1075]},            
                {bounds:[547,930,682,975],balance:[455,973,568,1075]},
                {bounds:[221,930,356,975],balance:[129,973,242,1075]},
            ],
            //秒看剧场
            "浮光剧场": [
                {bounds:[873,1147,1008,1192],balance:[759,1190,917,1292]},             
                {bounds:[547,1147,682,1192],balance:[433,1190,591,1292]}, 
                {bounds:[221,1147,356,1192],balance:[129,1190,242,1292]},                     
                {bounds:[873,930,1008,975],balance:[781,973,894,1075]},            
                {bounds:[547,930,682,975],balance:[455,973,568,1075]},
                {bounds:[217,930,356,975],balance:[129,973,242,1075]},
            ],
            "秒看剧场": [
                {bounds:[873,1147,1008,1192],balance:[759,1190,917,1292]},             
                {bounds:[529,1147,682,1192],balance:[433,1190,591,1292]}, 
                {bounds:[203,1147,356,1192],balance:[129,1190,242,1292]},                     
                {bounds:[855,930,1008,975],balance:[781,973,894,1075]},            
                {bounds:[529,930,682,975],balance:[455,973,568,1075]},
                {bounds:[203,930,356,975],balance:[129,973,242,1075]},
            ],
            "来点短剧": [
                {bounds:[863,1122,1010,1166],balance:[762,1166,917,1266]}, 
                {bounds:[535,1122,682,1166],balance:[456,1166,567,1266]},
                {bounds:[208,1122,355,1166],balance:[129,1166,240,1266]},
                {bounds:[863,904,1010,948],balance:[784,948,895,1048]},
                {bounds:[535,904,682,948],balance:[456,948,567,1048]}, 
                {bounds:[208,904,355,948],balance:[129,948,240,1048]},
            ],
            "金剧通": [
                {bounds:[863,1122,1010,1166],balance:[762,1166,917,1266]}, 
                {bounds:[535,1122,682,1166],balance:[456,1166,567,1266]},
                {bounds:[208,1122,355,1166],balance:[129,1166,240,1266]},
                {bounds:[863,904,1010,948],balance:[784,948,895,1048]},
                {bounds:[535,904,682,948],balance:[456,948,567,1048]}, 
                {bounds:[222,904,355,948],balance:[129,948,240,1048]},
            ],
            "浮云短剧": [
                {bounds:[855,1147,1008,1192],balance:[759,1190,917,1292]},             
                {bounds:[529,1147,682,1192],balance:[455,1190,568,1292]}, 
                {bounds:[203,1147,356,1192],balance:[129,1190,242,1292]},                     
                {bounds:[855,930,1008,975],balance:[781,973,894,1075]},            
                {bounds:[529,930,682,975],balance:[455,973,568,1075]},
                {bounds:[217,930,356,975],balance:[129,973,242,1075]},
            ],
            "赚剧时刻": [
                {bounds:[855,1147,1008,1192],balance:[781,1190,894,1292]},             
                {bounds:[529,1147,682,1192],balance:[455,1190,568,1292]}, 
                {bounds:[203,1147,356,1192],balance:[129,1190,242,1292]},                     
                {bounds:[855,930,1008,975],balance:[759,973,917,1075]},            
                {bounds:[529,930,682,975],balance:[433,973,591,1075]},
                {bounds:[217,930,356,975],balance:[129,973,242,1075]},
            ]
        };

        // 根据游戏名称选择对应的boundsArray，如果没有匹配则使用默认配置
        var boundsArray = gameBoundsConfig[gameConfig.名称] || gameBoundsConfig["默认"];
        Logger.debug(module, "使用游戏 '" + gameConfig.名称 + "' 的坐标配置，共 " + boundsArray.length + " 个提现选项");

        // 遍历boundsArray寻找可提现的金额
        var clicked = false;
        for (var i = 0; i < boundsArray.length; i++) {
            var item = boundsArray[i];

            // 提取文本 - 解构数组作为参数以确保坐标正确转换
            var timesResult = extractTextByUI(item.bounds[0], item.bounds[1], item.bounds[2], item.bounds[3]);  // 次数
            var amountResult = extractTextByUI(item.balance[0], item.balance[1], item.balance[2], item.balance[3]); // 提现金额选项
            
            // 安全处理文本结果
            var timesText = (timesResult && timesResult.success && timesResult.text) ? timesResult.text : "";
            var amountText = (amountResult && amountResult.success && amountResult.text) ? amountResult.text : "";
            
            // 从金额文本中提取数字
            var amountTextClean = amountText.replace(/[^\d.]/g, ''); // 只保留数字和小数点
            var withdrawAmount = parseFloat(amountTextClean) || 0;
            
            Logger.debug(module, "检查提现选项[" + i + "]: 次数='" + timesText + "', 金额=" + withdrawAmount + " (原始金额文本: '" + amountText + "')");
            
            // 检查余额是否足够
            if (currentBalance >= withdrawAmount && withdrawAmount > 0) {
                if (timesText && timesText.indexOf("0") === -1) {
                    // 文本不包含"0"，且余额足够，点击相应区域
                    // 从item.balance数组中提取中点坐标作为点击位置（基础分辨率）
                    // humanClick函数内部会自动处理坐标转换，无需提前转换
                    var baseX = Math.floor((item.balance[0] + item.balance[2]) / 2);
                    var baseY = Math.floor((item.balance[1] + item.balance[3]) / 2);
                    Logger.debug(module, "点击提现选项: 金额=" + withdrawAmount + ", 坐标=(" + baseX + ", " + baseY + ")");
                    humanClick(baseX, baseY); // 提现区域，
                    clicked = true;
                    break;
                }
            }
        }
        
        // 如果没有找到合适的提现金额，检查是否余额不足
        if (!clicked){
            humanClick(189,1007); // 第1个
            return false;
        }
        randomSleep(3000,5200);  
        gameClick(573, 1687); // 提现至微信
        randomSleep(3000,5200);   
        gameClick(921, 773); // 加速提现关闭(余额不足时弹出)
        randomSleep(3000,5200);    
        gameClick(523, 1481); // 开心收下
        randomSleep(3000,5200);    
        gameClick(531, 1209); // 立即抽奖
        randomSleep(3000,5200);    
        gameClick(621, 1502); // 开心收下
        randomSleep(3000,5200);  
        gameClick(79, 175); // 返回箭头
        Logger.debug(module, "我爱短剧函数执行完成");
        return true;
    } catch (e) {
        Logger.debug(module, "我爱短剧执行异常: " + e.message);
        // 发生异常，返回false表示未执行提现
        randomSleep(2000,3000);
        return false;
    }
}
